package com.nycreativity.elecreativity.controller


import com.nycreativity.elecreativity.aop.ValidateAspect
import com.nycreativity.elecreativity.entity.ResultData
import com.nycreativity.elecreativity.entity.cons.CommonURLConstants
import com.nycreativity.elecreativity.entity.param.brand.BrandAddParam
import com.nycreativity.elecreativity.entity.param.brand.BrandUpdateParam
import com.nycreativity.elecreativity.entity.vo.brand.BrandVO
import com.nycreativity.elecreativity.service.BrandService
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.springframework.validation.BindingResult
import org.springframework.web.bind.annotation.*

import javax.annotation.Resource
import javax.validation.Valid

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sxt && ttxxi
 * @since 2021-03-11
 */
@RestController
@RequestMapping(CommonURLConstants.BRAND_PREFIX)
@Api(tags = ["品牌相关接口"])
class BrandController {

    @Resource
    lateinit var brandService: BrandService

    @GetMapping
    @ApiOperation("根据id查询")
    fun getById(id: Long): ResultData<BrandVO> {
        val brandVO = brandService.queryById(id)
        return ResultData.SUCCESS(data = brandVO)
    }

    @GetMapping("/all")
    @ApiOperation("查询全部")
    fun getAll(): ResultData<List<BrandVO>> {
        val list = brandService.queryAll()
        return ResultData.SUCCESS(data = list)
    }

    @PostMapping
    @ApiOperation("创建品牌信息")
    @ValidateAspect.ParamValidate
    fun addBrand(@RequestBody @Valid brandAddParam: BrandAddParam, bindingResult: BindingResult): ResultData<Any> {
        val res = brandService.add(brandAddParam)
        return if (res) {
            ResultData.SUCCESS()
        } else {
            ResultData.FAILED("请检查参数是否正确")
        }
    }

    @PutMapping
    @ApiOperation("根据id修改品牌")
    @ValidateAspect.ParamValidate
    fun updateById(
        @RequestBody @Valid brandUpdateParam: BrandUpdateParam,
        bindingResult: BindingResult
    ): ResultData<Any> {
        val res = brandService.updateById(brandUpdateParam)
        return if (res) {
            ResultData.SUCCESS()
        } else {
            ResultData.FAILED("请检查参数是否正确")
        }
    }

    @DeleteMapping
    @ApiOperation("根据id删除")
    fun deleteById(id: Long): ResultData<Any> {
        val res = brandService.deleteById(id)
        return if (res) {
            ResultData.SUCCESS()
        } else {
            ResultData.FAILED("该brand下仍有goods，无法删除")
        }
    }


}

